// pages/authorized-login/authorized-login.js
import { URL, SER_KEY } from "../../config.js"
import { POST, GET } from "../../utils/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    session_id:'',
    type:null,
    productId:null,
    shopName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      type: options.type,
      productId: options.id
    })
    that.session_id?'':that.login()
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        console.log(res)
        let sessionId = wx.getStorageSync('session_id')
        if (res.authSetting['scope.userInfo'] && sessionId) {
          wx.getUserInfo({
            success: function (res) {
              //从数据库获取用户信息
              //这里我使用queryUsreInfo函授获取信息，你要换成你的，或者不用
              //that.queryUsreInfo();
              //用户已经授权过
              console.log(res)
              wx.switchTab({
                // url: '../index/index'
              })
            } 
          });
        } else {
          //用户没有授权
         return;
        }
      }
    })
  },
  login(){
    // 登录
    wx.login({
      success: res => {
        let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY0;
        let data = {
          code: res.code,
          appId: getApp().globalData.appId
        };
        GET(url, data, (resData) => {
          this.setData({
            session_id: resData.data.session_id
          })
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let shopData=JSON.parse(wx.getStorageSync('shopData'))
    console.log(shopData)
    this.setData({
      shopName:shopData.shop_name
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      //授权成功后，跳转进入小程序首页
      console.log(e.detail.userInfo)
      let encryptedData = encodeURIComponent(e.detail.encryptedData)
      let iv = e.detail.iv
      let sessionId = that.data.session_id
      
      console.log()
      that.UpdateUserInfo(encryptedData, iv, sessionId)

    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  UpdateUserInfo(encryptedData, iv, sessionId){
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY2;
    let data = {
      sessionId: sessionId,
      appId: getApp().globalData.appId,
      encryptedData: encryptedData,
      iv: iv
    };
    GET(url, data, (res) => {
      console.log(res);
      wx.setStorageSync('session_id', res.data.session_id)
      let type=this.data.type
      if(type==0){
          wx.navigateTo({
            url: '../product-details/product-details?id='+this.data.productId,
          })
      }else{
        wx.switchTab({
          url: '../index/index'
        })
      }
      
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})